//
//  IntegralController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/21.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "IntegralController.h"
#import "HYSegmentControl.h"
#import "HHTextFiledView.h"

@interface IntegralController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIView *whiteContainerView;
@property (nonatomic, strong) UILabel *currentIntegralL;
@property (nonatomic, strong) UILabel *canChangeIntegralL;
@property (nonatomic, strong) UILabel *currentIntegralNumberL;
@property (nonatomic, strong) UILabel *canChangeIntegralNumberL;
@property (nonatomic, strong) UIView *verticalV;
@property (nonatomic, strong) UIView *currentIntegralView;
@property (nonatomic, strong) UIView *canChangeIntegralView;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIImageView *confirmIV;
@property (nonatomic, strong) UILabel *confirmL;

@property (nonatomic, strong) UIView *oneView;
@property (nonatomic, strong) UIView *twoView;


@property (nonatomic, weak) UILabel *bottomOneLabel;
@property (nonatomic, weak) UILabel *bottomTwoLabel;

@property (nonatomic, strong) HHTextFiledView *field1;
@property (nonatomic, strong) HHTextFiledView *field2;
@property (nonatomic, strong) HHTextFiledView *field3;
@property (nonatomic, strong) HHTextFiledView *field4;

@property (nonatomic, assign) NSInteger selectIndex;

@property (nonatomic, weak) UILabel *exchangeBLlabel;
@property (nonatomic, weak) UILabel *exchangeNumlabel;


@end

@implementation IntegralController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectIndex = 0;
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = Color333333;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        CGFloat topHeight = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(430) : jisuan(380);
        make.height.mas_equalTo(topHeight);
    }];
    
    [topView addSubview:self.whiteContainerView];
    [self.whiteContainerView addSubview:self.currentIntegralView];
    [self.whiteContainerView addSubview:self.canChangeIntegralView];
    [self.currentIntegralView addSubview:self.currentIntegralL];
    [self.currentIntegralView addSubview:self.currentIntegralNumberL];
    [self.canChangeIntegralView addSubview:self.canChangeIntegralL];
    [self.canChangeIntegralView addSubview:self.canChangeIntegralNumberL];
    [topView addSubview:self.verticalV];
    
    WS(weakSelf);
    [self.whiteContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(jisuan(200));
        make.left.mas_equalTo(topView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(topView.mas_right).with.offset(-jisuan(20));
        make.bottom.mas_equalTo(topView.mas_bottom).with.offset(-jisuan(20));
        
    }];
    
    CGFloat width = (WIDTH - 2 * jisuan(20)) / 2.0;
    
    [self.currentIntegralView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.whiteContainerView.mas_left);
        make.top.mas_equalTo(weakSelf.whiteContainerView.mas_top);
        make.bottom.mas_equalTo(weakSelf.whiteContainerView.mas_bottom);
        make.width.mas_equalTo(width);
        
    }];
    [self.canChangeIntegralView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.whiteContainerView.mas_right);
        make.top.mas_equalTo(weakSelf.whiteContainerView.mas_top);
        make.bottom.mas_equalTo(weakSelf.whiteContainerView.mas_bottom);
        make.width.mas_equalTo(width);
        
    }];
    [self.currentIntegralL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.currentIntegralView.mas_centerX);
        make.top.mas_equalTo(weakSelf.currentIntegralView.mas_top).with.offset(jisuan(40));
        
    }];
    [self.currentIntegralNumberL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.currentIntegralView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.currentIntegralView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.currentIntegralL.mas_bottom).with.offset(jisuan(30));
        
    }];
    
    [self.canChangeIntegralL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.canChangeIntegralView.mas_centerX);
        make.top.mas_equalTo(weakSelf.canChangeIntegralView.mas_top).with.offset(jisuan(40));
        
    }];
    [self.canChangeIntegralNumberL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.canChangeIntegralView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.canChangeIntegralView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.canChangeIntegralL.mas_bottom).with.offset(jisuan(30));
        
    }];
    
    
    [self.verticalV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(jisuan(60));
        make.width.mas_equalTo(jisuan(2));
        make.centerY.mas_equalTo(weakSelf.whiteContainerView.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.whiteContainerView.mas_centerX);
        
    }];
    
    // 创建
    NSMutableArray *titleSourcd = [NSMutableArray arrayWithObjects:@"积分兑换趣豆", @"趣豆兑换积分", nil];
    HYSegmentControl *segmentControl = [HYSegmentControl segmentColor:CGRectMake(0, jisuan(450), self.view.bounds.size.width, 30) titleSource:titleSourcd titleColor:Color333333 selectTitleColor:Color333333 titleFont:[UIFont systemFontOfSize:13] selectTiteFont:[UIFont fontWithName:@"Helvetica-Bold" size:13] backgroundColor:[UIColor whiteColor] bottomLingColor:[UIColor redColor]];
    
    segmentControl.finished = ^(NSInteger btnTag) {
        if (btnTag == 0) {
            weakSelf.twoView.hidden = YES;
            weakSelf.oneView.hidden = NO;
            weakSelf.selectIndex = 0;
            [weakSelf loadDataWithTop:@"1"];
            
        } else {
            weakSelf.twoView.hidden = NO;
            weakSelf.oneView.hidden = YES;
            weakSelf.selectIndex = 1;
            [weakSelf loadDataWithTop:@"2"];
        }
    };
    [self.view addSubview:segmentControl];    
    
    self.oneView = [[UIView alloc] init];
    [self.view addSubview:self.oneView];
    [self.oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(segmentControl.mas_bottom).offset(15);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(100);
    }];
    
    self.twoView = [[UIView alloc] init];
    self.twoView.hidden = YES;
    [self.view addSubview:self.twoView];
    [self.twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(segmentControl.mas_bottom).offset(15);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(100);
    }];
    
    
    HHTextFiledView *oneonetextview = [[HHTextFiledView  alloc] initwithTitle:@"积分兑换比例" Place:@"请输入使用多少积分" Label:@"兑换1趣豆"];
    oneonetextview.field.delegate = self;
    oneonetextview.field.keyboardType = UIKeyboardTypeNumberPad;
    [self.oneView addSubview:oneonetextview];
    [oneonetextview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    self.field1 = oneonetextview;
    
    HHTextFiledView *onetwotextview = [[HHTextFiledView  alloc] initwithTitle:@"积分使用数量" Place:@"请输入需要兑换的积分个数" Label:@""];
    [self.oneView addSubview:onetwotextview];
    [onetwotextview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    self.field2 = onetwotextview;
    
    HHTextFiledView *twoone = [[HHTextFiledView  alloc] initwithTitle:@"1趣豆兑换" Place:@"" Label:@"个积分"];
    twoone.field.delegate = self;
    twoone.field.keyboardType = UIKeyboardTypeNumberPad;
    [self.twoView addSubview:twoone];
    [twoone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    self.field3 = twoone;
    
    HHTextFiledView *twotwo = [[HHTextFiledView  alloc] initwithTitle:@"兑换积分数量" Place:@"请输入需要兑换的积分数量" Label:@""];
    [self.twoView addSubview:twotwo];
    [twotwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    self.field4 = twotwo;
    
    
    UIImageView *imagevvv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xingzhuang"]];
    [self.view addSubview:imagevvv];
    [imagevvv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(weakSelf.oneView.mas_bottom).offset(jisuan(120));
        make.height.mas_equalTo(jisuan(250));
    }];
    
    
    UIView *vvvvv = [[UIView alloc] init];
//    vvvvv.backgroundColor = [UIColor grayColor];
    [imagevvv addSubview:vvvvv];
    [vvvvv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.right.equalTo(imagevvv.mas_centerX);
        make.height.mas_equalTo(jisuan(140));
    }];
    
    
    UIView *vllllll = [[UIView alloc] init];
//    vllllll.backgroundColor = [UIColor grayColor];
    [imagevvv addSubview:vllllll];
    [vllllll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
        make.left.equalTo(imagevvv.mas_centerX);
        make.height.mas_equalTo(jisuan(140));
    }];
    
    
    UILabel *labelllll = [[UILabel alloc] init];
    labelllll.text = @"当前趣豆兑换比例";
    labelllll.font = Font(24);
    labelllll.textColor = Color999999;
    [vvvvv addSubview:labelllll];
    [labelllll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(vvvvv);
        make.bottom.mas_equalTo(0);
    }];
    self.exchangeBLlabel = labelllll;
    
    UILabel *labeaaaaa = [[UILabel alloc] init];
    labeaaaaa.text = @"当前趣豆匹配数";
    labeaaaaa.font = Font(24);
    labeaaaaa.textColor = Color999999;
    [vllllll addSubview:labeaaaaa];
    [labeaaaaa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(vllllll);
        make.bottom.mas_equalTo(0);
    }];
    self.exchangeNumlabel = labeaaaaa;
    
    UILabel *ppppl = [[UILabel alloc] init];
    ppppl.textColor = Colorff0000;
    ppppl.text = @"10.44积分";
    ppppl.font = Font(48);
    [vvvvv addSubview:ppppl];
    [ppppl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(vvvvv);
        make.bottom.equalTo(labelllll.mas_top).offset(-5);
    }];
    self.bottomOneLabel = ppppl;
    
    UILabel *pppplaa = [[UILabel alloc] init];
    pppplaa.textColor = Colorff0000;
    pppplaa.text = @"10.44积分";
    pppplaa.font = Font(48);
    [vllllll addSubview:pppplaa];
    [pppplaa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(vllllll);
        make.bottom.equalTo(labeaaaaa.mas_top).offset(-5);
    }];
    self.bottomTwoLabel = pppplaa;
    
    
    [self.view addSubview:self.confirmIV];
    [self.confirmIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
//        make.top.equalTo(imagevvv.mas_bottom).offset(jisuan(100));
        make.bottom.mas_equalTo(-Scale_W(20));
        make.size.mas_equalTo(CGSizeMake(jisuan(550), jisuan(106)));
    }];
    
    [self.confirmIV addSubview:self.confirmL];
    [self.confirmL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.confirmIV.mas_centerY).offset(-5);
        make.centerX.equalTo(self.confirmIV.mas_centerX);
    }];

    [self loadDataWithTop:@"1"];
    
}

- (void)loadDataWithTop:(NSString *)index {
    WS(weakSelf);
    NSString *string = [NSString stringWithFormat:@"{\"user_id\" : \"%@\",\"type\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id  publicKey:PublicKey],[RSA encryptString:index publicKey:PublicKey]];
    [PPNetworkHelper POST:@"transaction/Index/index" parameters:@{@"json":string} success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 1) {
            [weakSelf loadWithBottomData:responseObject[@"data"]];
        }
    } failure:^(NSError *error) {
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}




- (void)loadWithBottomData:(NSDictionary *)dic {
    
    NSString *str = [NSString stringWithFormat:@"%@积分",dic[@"bean_bili"]];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:str];
    [attribute addAttribute:NSFontAttributeName value:Font(60) range:NSMakeRange(0, str.length - 2)];
    [attribute addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(str.length - 2, 2)];
    self.bottomOneLabel.attributedText = attribute;
    
    NSString *str2 = [NSString stringWithFormat:@"%@分",dic[@"jifen_count"]];
    NSMutableAttributedString *attribute2 = [[NSMutableAttributedString alloc] initWithString:str2];
    [attribute2 addAttribute:NSFontAttributeName value:Font(60) range:NSMakeRange(0, str2.length - 1)];
    [attribute2 addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(str2.length - 1, 1)];
    self.bottomTwoLabel.attributedText = attribute2;
    
    self.currentIntegralNumberL.text = [NSString stringWithFormat:@"%@", dic[@"user_bean"]];
    self.canChangeIntegralNumberL.text = [NSString stringWithFormat:@"%@", dic[@"bean_quota"]];
    self.exchangeBLlabel.text = [NSString stringWithFormat:@"%@", dic[@"font_bili"]];
    self.exchangeNumlabel.text = [NSString stringWithFormat:@"%@", dic[@"font_count"]];
    
    
}

- (void)confirmGestureaaaaa:(UITapGestureRecognizer *)tap {
    
    if (self.selectIndex == 0) {
        [self qudouduihuanjifen];
    } else {
        [self jifenduihuanqudou];
    }
}
/**  趣豆兑换积分
 */
- (void)jifenduihuanqudou {
    
    if (self.field3.field.text.length < 1) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的个数"];
    }
    
    if (self.field4.field.text.length < 1) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的数量"];
    }
    
    if ([self.field3.field.text floatValue] <= 0) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的个数"];
    }
    
    if ([self.field4.field.text floatValue] <= 0) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的数量"];
    }
    WS(weakSelf);
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"unit\" : \"%@\", \"count\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id  publicKey:PublicKey],[RSA encryptString:self.field3.field.text  publicKey:PublicKey],[RSA encryptString:self.field4.field.text  publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"transaction/Index/integralaction" parameters:@{@"json":par} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            weakSelf.field3.field.text = @"";
            weakSelf.field4.field.text = @"";
        }
        [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        [weakSelf loadDataWithTop:self.selectIndex==0?@"1":@"2"];
    } failure:^(NSError *error) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
    }];
}

/**积分兑换趣豆
 */
- (void)qudouduihuanjifen {
    
    if (self.field1.field.text.length < 1) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入使用多少积分"];
    }
    
    if (self.field2.field.text.length < 1) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的个数"];
    }
    
    if ([self.field1.field.text floatValue] <= 0) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入使用多少积分"];
    }
    
    if ([self.field2.field.text floatValue] <= 0) {
        return [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入需要兑换积分的个数"];
    }
    WS(weakSelf);
 NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"unit\" : \"%@\", \"count\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id  publicKey:PublicKey],[RSA encryptString:self.field1.field.text  publicKey:PublicKey],[RSA encryptString:self.field2.field.text  publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"transaction/Index/beanaction" parameters:@{@"json":par} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            weakSelf.field1.field.text = @"";
            weakSelf.field2.field.text = @"";
        }
        [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        [weakSelf loadDataWithTop:self.selectIndex==0?@"1":@"2"];
    } failure:^(NSError *error) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
    }];
}


- (UIImageView *)confirmIV {
    
    if (!_confirmIV) {
        _confirmIV = [[UIImageView alloc] init];
        _confirmIV.contentMode = UIViewContentModeScaleAspectFit;
        _confirmIV.image = [UIImage imageNamed:@"redBtnBG"];
        _confirmIV.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(confirmGestureaaaaa:)];
        [_confirmIV addGestureRecognizer:gesture];
        
    }
    
    return _confirmIV;
}

- (UILabel *)confirmL {
    
    if (!_confirmL) {
        _confirmL = [[UILabel alloc] init];
        _confirmL.text = @"确认兑换";
        _confirmL.textColor = WhiteColor;
        _confirmL.textAlignment = NSTextAlignmentCenter;
        _confirmL.font = Font(32);
    }
    
    return _confirmL;
    
}



- (UIView *)whiteContainerView {
    
    if (!_whiteContainerView) {
        
        _whiteContainerView = [[UIView alloc] init];
        _whiteContainerView.backgroundColor = WhiteColor;
        
        _whiteContainerView.layer.cornerRadius = jisuan(10);
        _whiteContainerView.layer.masksToBounds = YES;
        
    }
    
    return _whiteContainerView;
}

- (UILabel *)currentIntegralL {
    
    if (!_currentIntegralL) {
        _currentIntegralL = [[UILabel alloc] init];
        _currentIntegralL.textColor = Color999999;
        _currentIntegralL.font = Font(24);
        _currentIntegralL.textAlignment = NSTextAlignmentCenter;
        _currentIntegralL.text = @"当前积分";
    }
    return _currentIntegralL;
}

- (UILabel *)currentIntegralNumberL {
    
    if (!_currentIntegralNumberL) {
        
        _currentIntegralNumberL = [[UILabel alloc] init];
        _currentIntegralNumberL.textColor = Color1f89ff;
        _currentIntegralNumberL.font = Font(60);
        _currentIntegralNumberL.textAlignment = NSTextAlignmentCenter;
    }
    
    return _currentIntegralNumberL;
}

- (UILabel *)canChangeIntegralL {
    
    if (!_canChangeIntegralL) {
        _canChangeIntegralL = [[UILabel alloc] init];
        _canChangeIntegralL.textColor = Color999999;
        _canChangeIntegralL.font = Font(24);
        _canChangeIntegralL.textAlignment = NSTextAlignmentCenter;
        _canChangeIntegralL.text = @"趣豆";
    }
    
    return _canChangeIntegralL;
}

- (UILabel *)canChangeIntegralNumberL {
    
    if (!_canChangeIntegralNumberL) {
        
        _canChangeIntegralNumberL = [[UILabel alloc] init];
        _canChangeIntegralNumberL.textColor = Colorff0000;
        _canChangeIntegralNumberL.font = Font(60);
        _canChangeIntegralNumberL.textAlignment = NSTextAlignmentCenter;
    }
    
    return _canChangeIntegralNumberL;
}

- (UIView *)verticalV {
    
    if (!_verticalV) {
        _verticalV = [[UIView alloc] init];
        _verticalV.backgroundColor = Colordddddd;
    }
    return _verticalV;
}

- (UIView *)currentIntegralView {
    
    if (!_currentIntegralView) {
        _currentIntegralView = [[UIView alloc] init];
        _currentIntegralView.backgroundColor = WhiteColor;
    }
    return _currentIntegralView;
}
- (UIView *)canChangeIntegralView {
    
    if (!_canChangeIntegralView) {
        _canChangeIntegralView = [[UIView alloc] init];
        _canChangeIntegralView.backgroundColor = WhiteColor;
    }
    return _canChangeIntegralView;
}

- (UIView *)bottomLine {
    
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = Colordddddd;
    }
    return _bottomLine;
}


@end
